package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.lp.LuckPermsTown;

import java.util.Collections;
import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;
import static org.mclll.xwangplugin.util.PlayerTools.getOfflineOrOnlinePlayerByName;

/**
 * @author: geduo
 * @date: 2023/9/23 19:33
 */
public class DeployPlayerExecutor {

    public static void deployPlayer(CommandSender sender, String[] args) {
        // poitown deploy <玩家名> <城镇名> <confirm>
        if (args.length < 3) {
            sender.sendMessage(DEPLOY_PLAYER_COMMAND_FORMAT);
            return;
        }
        if (args.length == 3) {
            // 二次确认踢出玩家
            sender.sendMessage(
                    Component.text(String.format(DEPLOY_PLAYER_CONFIRM, args[1], args[2]))
                    .appendNewline()
                    .append(
                            Component.text(CONFIRM_BUTTON)
                                    .clickEvent(ClickEvent.runCommand(String.format("/poitown deploy %s %s confirm", args[1], args[2])))
                                    .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                    )
                    .append(Component.text("         "))
                    .append(Component.text(CANCEL_BUTTON).hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON))))
            );
            return;
        }
        if (args.length != 4) {
            return;
        }
        if (!"confirm".equals(args[3])) {
            sender.sendMessage(DEPLOY_PLAYER_COMMAND_FORMAT);
            return;
        }
        String playerName = args[1].trim();
        String townName = args[2].trim();
        TownDO townParam = new TownDO();
        townParam.setName(townName);
        List<TownDO> townList = TownManage.selectList(townParam);
        if (townList.isEmpty()) {
            sender.sendMessage(TOWN_NOT_FOUND);
            return;
        }
        TownDO town = townList.get(0);
        TownPlayerDO playerParam = new TownPlayerDO();
        playerParam.setName(playerName);
        List<TownPlayerDO> townPlayerList = TownPlayerManage.selectList(playerParam);
        OfflinePlayer player = getOfflineOrOnlinePlayerByName(playerName);
        boolean success = false;
        if (!townPlayerList.isEmpty()) {
            playerParam = townPlayerList.get(0);
            if (playerParam.getIdentity() == PlayerTownIdentity.LEADER) {
                sender.sendMessage(PLAYER_IS_ALREADY_LEADER);
                return;
            }
            playerParam.setIdentity(PlayerTownIdentity.MEMBER);
            playerParam.setTownUuid(town.getUuid());
            TownPlayerManage.updateByUuid(playerParam);
            success = true;
        } else {
            sender.sendMessage(PLAYER_NOT_FOUND);
        }
        if (success) {
            // 权限部分
            LuckPermsTown.addPlayerToTown(townName, Collections.singletonList(playerName));
            sender.sendMessage(String.format(DEPLOY_PLAYER_SUCCESS, playerName, townName));
            if (player.isOnline()) {
                ((Player) player).sendMessage(String.format(DEPLOY_PLAYER_SUCCESS_SEND_PLAYER, townName));
            }
        }
    }

    public static List<String> deployPlayerTabComplete(CommandSender sender, String[] args) {
        if (args.length == 3) {
            return TownManage.selectList(null).stream()
                    .map(TownDO::getName)
                    .filter(townName -> townName.startsWith(args[1]))
                    .collect(java.util.stream.Collectors.toList());
        }
        return null;
    }
}
